//
//  XWTopViewController.m
//  CGWSWW
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 CXG. All rights reserved.
//

#import "XWTopViewController.h"

#import "HeadHelp.h"
#import "XWNewsCell.h"
#import <MJRefresh.h>
#import <AFNetworking.h>
#import "XWNewsEntity.h"
#import <MJExtension.h>

@interface XWTopViewController ()
//数据数组
@property (nonatomic , strong) NSMutableArray *totalArray;

@property (nonatomic , strong) NSMutableArray *topArray;
@property (nonatomic , strong) NSMutableArray *titleArray;
@property (nonatomic , strong) NSMutableArray *imagesArray;
@end

@implementation XWTopViewController
#pragma mark 懒加载
-(NSMutableArray *)totalArray
{
    if (!_totalArray) {
        _totalArray = [NSMutableArray array];
    }
    return _totalArray;
}
-(NSMutableArray *)imagesArray
{
    if (!_imagesArray) {
        _imagesArray = [NSMutableArray array];
    }
    return _imagesArray;
}
-(NSMutableArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}
-(NSMutableArray *)topArray
{
    if (!_topArray) {
        _topArray = [NSMutableArray array];
    }
    return _topArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化tableview
     [self initTopView];

   
//    //刷新控件
    [self setupRefreshView];
    
    
}

-(void)setupRefreshView{
    __weak XWTopViewController *weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    self.tableView.mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];


}
//头部滚动视图数据
-(void)initTopView{
     //网易顶部滚动
     //   http://c.m.163.com/nc/article/headline/T1348647853363/0-1.html
     
    AFHTTPSessionManager *marger = [AFHTTPSessionManager manager ];
    [marger GET:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-10.html" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.topArray = [XWNewsEntity mj_objectArrayWithKeyValuesArray:responseObject[@"T1348647853363"][0][@"ads"]];
     //创建frame对象在滚动视图上呈现
     
//        for (XWNewsEntity *entityData in self.topArray) {
//
//            [self.titleArray addObject:entityData.title];
//            [self.imagesArray addObject:entityData.imgsrc];
//            
//        }
           [self initTableView];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"广告数据解析失败");
    }];
    
}


//下拉刷新
-(void)loadData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
        NSLog(@"hh");
    });
}
//上拉刷新
-(void)loadMoreData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
        NSLog(@"ii");
    });
}
//初始化tableview
-(void)initTableView{
    self.tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
    self.tableView.scrollIndicatorInsets =UIEdgeInsetsMake(44, 0, 0, 0);
    self.view.backgroundColor =XWCommonColor;


}
#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell  =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"];
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
